package Dao.Impl;

import Dao.CommentDao;
import Domain.Comment;
import JDBCUtils.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CommentDaoImpl implements CommentDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Comment> FindCommentByPid(int pid) {
        String sql = "select\n" +
                " t.id , t2.username , t1.name , t.newmsg , t.picture , t.remsg , t.newtime , t.retime , t.ckstatus , t.productid , t.userid\n" +
                "from\n" +
                " comment t\n" +
                "join\n" +
                " product t1\n" +
                "on\n" +
                " t.productid = t1.id\n" +
                "join\n" +
                " user_login t2\n" +
                "on\n" +
                " t.userid = t2.id\n" +
                "where\n" +
                " t.productid = ? and t.ckstatus = 'true';";
        List<Comment> result = null;
        try {
            result = template.query(sql,new BeanPropertyRowMapper<>(Comment.class),pid);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void AddComment(Comment comment) {
        String sql ="insert into comment set newmsg=?,picture=?,newtime=?,ckstatus=?,userid=?,productid=?";
        template.update(sql,comment.getNewmsg(),comment.getPicture(),comment.getNewtime(),comment.getCkstatus(),comment.getUserid(),comment.getProductid());
    }

    @Override
    public void DelComment(int id) {
        String sql ="delete from comment where id=?";
        template.update(sql,id);
    }
}
